Imports System.Diagnostics

Public MustInherit Class GenApp

    Public Shared Event ShowErrorMessage(ByVal sMessage As String)

#Region " Properties "

    Public Shared ReadOnly Property ApplicationName() As String
        Get
            If isWebApp() Then
                Return WebAppName
            Else
                Return WindowsAppName
            End If
        End Get
    End Property

    Public Shared ReadOnly Property Version() As String
        Get
            If isWebApp() Then
                Return WebAppVersion()
            Else
                Return WindowsAppVersion()
            End If
        End Get
    End Property

    Public Shared ReadOnly Property ImagesPrefixDirectory() As String
        Get
            'This works to evaluate the images for the themes.
            Return "~/"
        End Get
    End Property

    Public Shared ReadOnly Property ApplicationHomeDirectory() As String
        Get
            If isWebApp() Then
                Return System.AppDomain.CurrentDomain.BaseDirectory
            Else
                Return System.AppDomain.CurrentDomain.BaseDirectory
            End If
        End Get
    End Property

    Public Shared ReadOnly Property MachineName() As String
        Get
            Return System.Environment.MachineName
        End Get
    End Property

    Public Shared ReadOnly Property WebAdditionalConnectionString() As String
        Get
            Return ";Workstation ID=" + MachineName + ";Application Name=" + WebAppName
        End Get
    End Property

    Public Shared ReadOnly Property WindowsAdditionalConnectionString() As String
        Get
            Return ";Workstation ID=" + MachineName + ";Application Name=" + WindowsAppName + " v" + WindowsAppVersion
        End Get
    End Property

    Private Shared ReadOnly Property WebAppName() As String
        Get
            Dim s As String
            If Not System.Web.HttpContext.Current Is Nothing Then
                s = System.Web.HttpContext.Current.Request.ApplicationPath
            Else
                Return "WebApp"
            End If
            s = s.Replace("/", "")
            Return s
        End Get
    End Property

    Private Shared ReadOnly Property WindowsAppName() As String
        Get
            Return System.Windows.Forms.Application.ProductName
        End Get
    End Property

    Private Shared ReadOnly Property WindowsAppVersion() As String
        Get
            Return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()
        End Get
    End Property

    Private Shared ReadOnly Property WebAppVersion() As String
        Get
            Return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()
        End Get
    End Property

#End Region

#Region " Methods "

    Public Shared Function FormatAlertText(ByVal s As String) As String
        Dim sRet As String
        'Need to strip out "
        's = s.Replace("'", "")
        s = s.Replace("""", "")
        'Change vbcrlf to \n
        s = s.Replace(vbCrLf, "\n")
        sRet = "<script language=""javascript""> "
        sRet = sRet & "{ window.alert (""" & s & """); } "
        sRet = sRet & "</script" & ">"
        Return sRet
    End Function

    Public Shared Sub AlertUser(ByVal l As System.Web.UI.WebControls.Literal, ByVal s As String)
        l.Text = FormatAlertText(s)
    End Sub

    Public Shared Function AlertUser(ByVal sMessage As String) As Web.UI.Control
        Dim formLit As New System.Web.UI.WebControls.Literal
        Dim c As New System.Web.UI.Control
        formLit.Text = FormatAlertText(sMessage)
        c.Controls.Add(formLit)
        Return c
    End Function

    Public Shared Sub LogError(ByVal s As String)
        Dim oLog As New SysLog("Errors.log")
        '        oLog.SetLogMode(LogMode.MessageBox, True) Not necessary.
        oLog.LogText(s)
        RaiseEvent ShowErrorMessage(s)
    End Sub

    Public Shared Sub LogText(ByVal s As String)
        Dim oLog As New CommonUtils.SysLog
        'oLog.SetLogMode(CommonUtils.LogMode.Web, True)
        oLog.SetLogMode(CommonUtils.LogMode.File, True)
        oLog.LogText(s)
    End Sub

    Public Shared Sub LogText(ByVal s1 As String, ByVal s2 As String, ByVal ex As Exception)
        LogText(s1 + " - " + s2 + " - " + ex.Message)
    End Sub

    Public Shared Function isWebApp() As Boolean
        'checks the http.context object to see if it's null.  
        'if it's not, returns true.
        If System.Web.HttpContext.Current Is Nothing Then
            Return False
        Else
            Return True
        End If
    End Function

    Public Shared Function GetObjectFromStack(ByVal s As String) As String
        Return s
        ''The incoming string has a stack line in the format 
        ''"Construct at offset 281 in file:line:column c:\\documents and settings\\mbravo\\my documents\\visual studio projects\\foflibcs\\fundlendernav.cs:44:6\r\n"
        ''I want just the file name, which should be the object name.
        ''Split the string into an array, based on the :
        'Dim aParts() As String = New String() {}
        'Dim sFileSpec As String
        'Dim sFileName As String
        'aParts = s.Split(":")
        ''The filespec is the third array entry from the end.
        'sFileSpec = aParts(aParts.GetUpperBound(0) - 2)
        ''Split it into an array of nodes.
        'aParts = sFileSpec.Split("\\")
        ''The filename is the last entry in the array.
        'sFileName = aParts(aParts.GetUpperBound(0))
        ''Get rid of the .vb or .cs
        'sFileName = sFileName.Replace(".vb", "")
        'sFileName = sFileName.Replace(".cs", "")
        'Return sFileName
    End Function

    Public Shared Function GetObjectAndFunction() As String
        'Gets the calling object and function, returns as string.
        Dim s As String
        Dim sf As StackFrame = New System.Diagnostics.StackFrame(1, True)
        s = GetObjectFromStack(sf.ToString())
        s = s + ":" + sf.GetMethod.Name() + "()"
        Return s
    End Function

    Public Shared Function GetObjectName() As String
        'Gets the calling object
        Dim s As String
        Dim sf As StackFrame = New System.Diagnostics.StackFrame(1, True)
        s = GetObjectFromStack(sf.ToString())
        Return s
    End Function

    Public Shared Function GetWebPageName(ByVal sRequestURL As String) As String
        Dim aParts() As String = New String() {}
        Dim sFileName As String
        Dim s As String = sRequestURL
        aParts = s.Split("/")
        'The filename is the last entry in the array.
        sFileName = aParts(aParts.GetUpperBound(0))
        Return sFileName
    End Function


    Public Shared Function GetMethod() As String
        'Go one back in the stack to get the calling method name.
        Dim sf As StackFrame = New System.Diagnostics.StackFrame(1, True)
        Return sf.GetMethod.Name() + "()"
    End Function

#End Region

    Private Sub WriteFormValues(ByVal c As System.Web.HttpContext)
        Dim loop1 As Integer
        Dim coll As System.Collections.Specialized.NameValueCollection
        ' Load Header collection into NameValueCollection object.
        coll = c.Request.ServerVariables
        ' Put the names of all keys into a string array.
        Dim arr1() As String = coll.AllKeys
        For loop1 = 0 To arr1.Length - 1
            Dim sTest As String = arr1(loop1)
            If sTest.Length > 0 Then
                c.Response.Write(sTest)
                c.Response.Write("<BR>")
                Dim arr2() As String = coll.GetValues(arr1(loop1))
                Dim loop2 As Integer
                For loop2 = 0 To arr2.Length - 1
                    c.Response.Write(arr2(loop2))
                    c.Response.Write("<BR>")
                Next
            End If
        Next
    End Sub

End Class
